package com.app.car.dialog;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.app.car.R;
import com.app.car.basis.BasisActivity;
import com.app.car.bean.RfidListBean;
import com.app.car.check.info.CheckTabActivity;
import com.app.car.greendao.GreenDaoUtils;
import com.flyco.dialog.widget.NormalDialog;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.ObservableEmitter;
import io.reactivex.rxjava3.core.ObservableOnSubscribe;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.schedulers.Schedulers;
import my.ActivityTool;


/**
 * @作者 fourfaith
 * @备注 新闻详情图片查看activity发、
 */
public class CheckFinishDialog extends NormalDialog {


	@BindView(R.id.btn_out)
	TextView btn_out;
	@BindView(R.id.text_all)
	TextView text_all;    //账面数
	@BindView(R.id.text_check)
	TextView text_check;    //已盘数
	@BindView(R.id.text_pan_ying)
	TextView text_pan_ying;    //盘盈数
	@BindView(R.id.text_pan_kui)
	TextView text_pan_kui;    //盘亏数
	@BindView(R.id.view_save)
	RelativeLayout view_save;    //保存toast


	RfidListBean rfidListBean;
	BasisActivity mContext;


	public CheckFinishDialog(BasisActivity context, RfidListBean rfidListBean) {
		super(context);
		this.mContext = context;
		this.rfidListBean = rfidListBean;
		setCancelable(true);
		setCanceledOnTouchOutside(true);
	}

	@Override
	public View onCreateView() {
//        return super.onCreateView();
		View inflate = LayoutInflater.from(mContext).inflate(R.layout.check_list_item, null);
		ButterKnife.bind(this, inflate);
		widthScale(0.8f);
		return inflate;
	}

	@Override
	public void setUiBeforShow() {
		initViews();
		initData();
	}

	private void initViews() {
		btn_out.setText("保存");
	}

	private void initData() {
		text_all.setText(""+rfidListBean.getBookRfidList().size());
		text_check.setText(""+rfidListBean.getCheckRfidList().size());
		text_pan_ying.setText(""+rfidListBean.getPanYingList().size());
		text_pan_kui.setText(""+rfidListBean.getPanKuiList().size());
	}

	@OnClick({R.id.btn_out, R.id.btn_info})
	public void btnClick(View v) {
		switch (v.getId()) {
			case R.id.btn_out:
				toSave(0);
				break;
			case R.id.btn_info:
				toSave(1);
				break;

		}
	}

	public void toSave(int index){
		view_save.setVisibility(View.VISIBLE);
		Observable.create(new ObservableOnSubscribe<Integer>() {
			// 1\. 创建被观察者 & 生产事件
			@Override
			public void subscribe(ObservableEmitter<Integer> emitter) throws Exception {
//				rfidListBean.setTime(System.currentTimeMillis());
				GreenDaoUtils.rfidBeanInsertData(rfidListBean, new GreenDaoUtils.GreenDaoInserCallBack() {
					@Override
					public void onDataInsert(int count) {
						emitter.onNext(count);
					}
				});

				emitter.onComplete();
			}
		})   .subscribeOn(Schedulers.io())
				.observeOn(AndroidSchedulers.mainThread())
				.subscribe(new Observer<Integer>() {
					// 2\. 通过通过订阅（subscribe）连接观察者和被观察者
					// 3\. 创建观察者 & 定义响应事件的行为
					@Override
					public void onSubscribe(Disposable d) {

					}
					// 默认最先调用复写的 onSubscribe（）

					@Override
					public void onNext(Integer value) {
						dismiss();
						mContext.showToast("保存成功");

						if(index == 1){
							Bundle extras = new Bundle();
							extras.putLong("id",rfidListBean.getId());
							ActivityTool.skipActivity(mContext, CheckTabActivity.class,extras);
						}

					}

					@Override
					public void onError(Throwable e) {

					}

					@Override
					public void onComplete() {

					}
				});




	}


}
